package leecode

func minPathSum(grid [][]int) int {
	dp := make([][]int, len(grid)) //dp数组为走到每一个格子时的步数
	for i := 0; i < len(grid); i++ {
		dp[i] = make([]int, len(grid[i]))
	}
	dp[0][0] = grid[0][0]
	//初始化dp 每次只能向下或者向右
	for i := 1; i < len(grid); i++ {
		//每一行的第一列
		dp[i][0] = grid[i][0] + dp[i-1][0]
	}
	for j := 1; j < len(grid[0]); j++ {
		//第一行的每一列
		dp[0][j] = grid[0][j] + dp[0][j-1]
	}
	for i := 1; i < len(grid); i++ {
		for j := 1; j < len(grid[i]); j++ {
			//左边 上边
			dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + grid[i][j]
		}
	}
	return dp[len(dp)-1][len(dp[0])-1]
}
